package com.tom;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.mysql.jdbc.Driver;

public class Post {
	    int board_id;
        String userid;
        String nickname;
        String object;
        String boardDate;
        String content;
        ArrayList<Post> allPosts;
        
        
        public Post(){
                
        }
        
        
		public Post(int board_id, String userid, String nickname,
				String object, String boardDate, String content) {
			super();
			this.board_id = board_id;
			this.userid = userid;
			this.nickname = nickname;
			this.object = object;
			this.boardDate = boardDate;
			this.content = content;
		}


		public Post(int board_id, String userid, String nickname,
				String object, String content) {
			super();
			this.board_id = board_id;
			this.userid = userid;
			this.nickname = nickname;
			this.object = object;
			this.content = content;
		}

		



		public Post(String userid, String nickname, String object,
				String content) {
			super();
			this.userid = userid;
			this.nickname = nickname;
			this.object = object;
			this.content = content;
		}


		public int delete(int board_id){
                int rowCount = 0;
                try {
                        DriverManager.registerDriver(new Driver());
                        Connection conn = DriverManager.getConnection("jdbc:mysql://j.snpy.org/j102u05?useUnicode=true&characterEncoding=UTF-8",
                                        "j102u05", "j102u05.989");
                        String sql = "delete from boards where board_id=?";
                        PreparedStatement pstmt = conn.prepareStatement(sql);
                        pstmt.setInt(1, board_id);
                        rowCount = pstmt.executeUpdate();
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return rowCount;
        }
        
        public ArrayList<Post> getAllPosts(){
                if (allPosts==null)
                        allPosts = new ArrayList<Post>();
                allPosts.clear();
                try {
                        DriverManager.registerDriver(new Driver());
                        Connection conn = DriverManager.getConnection("jdbc:mysql://j.snpy.org/j102u05?useUnicode=true&characterEncoding=UTF-8",
                                        "j102u05", "j102u05.989");
                        String sql = "select * from boards";
                        PreparedStatement pstmt = conn.prepareStatement(sql);
                        ResultSet rs = pstmt.executeQuery();
                        while(rs.next()){
                                int id = rs.getInt("board_id");
                                String userid = rs.getString("m_userid");
                                String nickname = rs.getString("m_nickname");
                                String object = rs.getString("board_object");
                                String boardDate = rs.getString("board_Date");
                                String content = rs.getString("board_content");
                                Post p = new Post(id,userid, nickname,object,boardDate, content);
                                allPosts.add(p);
                        }
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                
                return allPosts;
        }
        
        public boolean save(){
                boolean saved = false;
                try {
                        DriverManager.registerDriver(new Driver());
                        Connection conn = DriverManager.getConnection("jdbc:mysql://j.snpy.org/j102u05?useUnicode=true&characterEncoding=UTF-8", "j102u05", "j102u05.989");
                        String sql = "insert into boards(m_userid, m_nickname, board_object, board_content) values (?,?,?,?)";
                        PreparedStatement pstmt = conn.prepareStatement(sql);
                        pstmt.setString(1, userid);
                        pstmt.setString(2, nickname);
                        pstmt.setString(3, object);
                        pstmt.setString(4, content);
                        int rowCount = pstmt.executeUpdate();
                        if (rowCount==1){
                                saved = true;
                        }
                } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return saved;
                
        }

        public String getUserid() {
                return userid;
        }
        public void setUserid(String userid) {
                this.userid = userid;
        }
        public String getNickname() {
                return nickname;
        }
        public void setNickname(String nickname) {
                this.nickname = nickname;
        }
        
        public String getContent() {
                return content;
        }
        public void setContent(String content) {
                this.content = content;
        }
        
        public void setAllPosts(ArrayList<Post> allPosts) {
                this.allPosts = allPosts;
        }

		public String getObject() {
			return object;
		}

		public void setObject(String object) {
			this.object = object;
		}

		public int getBoard_id() {
			return board_id;
		}

		public void setBoard_id(int board_id) {
			this.board_id = board_id;
		}


		public String getBoardDate() {
			return boardDate;
		}


		public void setBoardDate(String boardDate) {
			this.boardDate = boardDate;
		}
		       
}